package LinkedLists;

import LinkedLists.init.ListNode;

public class lc24 {
    /**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
    public ListNode swapPairs(ListNode head) {
        ListNode dum = new ListNode(0);
        dum.next = head;
        ListNode cur = dum;
        while (cur.next != null && cur.next.next != null) {
            ListNode first = cur.next;
            ListNode second = cur.next.next;

            // 交换节点
            first.next = second.next;
            second.next = first;
            cur.next = second;

            // 移动指针，处理下一个对
            cur = first;
        }
        return dum.next;
    }
}
